package Gui;

import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JButton;
import javax.swing.JTextField;

import client.ChatClient;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.util.StringTokenizer;

public class RemoveCourse extends JPanel
{

	public Secretry main_frame;
	private JTextField textField;
	String Massage;
	
	public RemoveCourse(Secretry a) 
	{
		setLayout(null);
		main_frame=a;
		JLabel lblRemoveCourse = new JLabel("Remove Course");
		lblRemoveCourse.setFont(new Font("Tahoma", Font.PLAIN, 20));
		lblRemoveCourse.setBounds(134, 22, 165, 25);
		add(lblRemoveCourse);
		
		JButton btnNewButton = new JButton("Next");
		
		
		btnNewButton.addActionListener(new ActionListener()
		{
			public void actionPerformed(ActionEvent e)
			{
				
				String course_id = textField.getText(); 
				Delete_course(course_id);
			}


		});
		
		
		btnNewButton.setBounds(330, 250, 89, 23);
		add(btnNewButton);
		
		JButton btnBack = new JButton("Back");
		
		btnBack.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent e) {
				main_frame.update_panel(3,2);
			}
		});
		
		
		btnBack.setBounds(233, 250, 89, 23);
		add(btnBack);
		
		JLabel lblEnterCourseInformation = new JLabel("Enter course information :");
		lblEnterCourseInformation.setFont(new Font("Tahoma", Font.BOLD, 14));
		lblEnterCourseInformation.setBounds(10, 100, 229, 25);
		add(lblEnterCourseInformation);
		
		JLabel lblCourseId = new JLabel("Course Id :");
		lblCourseId.setBounds(10, 151, 79, 25);
		add(lblCourseId);
		
		textField = new JTextField();
		textField.setBounds(99, 153, 86, 20);
		add(textField);
		textField.setColumns(10);
	}
	
	
	 public void Delete_course(String course_id) 
	 {
		
		String msg=new String("SELECT * FROM courses WHERE CourseID="+course_id);
		main_frame.client.handleMessageFromClientUI("6;"+msg); //6 number of the window/ transaction 
		Massage=main_frame.client.Massage;
		
		StringTokenizer st = new StringTokenizer (Massage, ";", false);
		if(Integer.parseInt(st.nextToken())==1)	//the delete successfully
		{
			showMsg1(st.nextToken());
			main_frame.update_panel(3, 1);
			textField.setText("");
		}
		else
		showMsg1(st.nextToken());
		
	 }
	 
		public void showMsg(String msg)		//Func that sent error message 
		{
			JOptionPane.showMessageDialog(null,msg,"Delete course",JOptionPane.ERROR_MESSAGE);
		}
		
		public void showMsg1(String msg)		//Func that sent successfully message 
		{
			JOptionPane.showMessageDialog(null,msg,"Delete course",JOptionPane.INFORMATION_MESSAGE);
		}
}
